package cn.community.system.service.impl;

import cn.community.system.domain.Association;
import cn.community.system.domain.Department;
import cn.community.system.mapper.AssociationMapper;
import cn.community.system.mapper.DepartmentMapper;
import cn.community.system.query.DepartmentQuery;
import cn.community.system.query.QueryObject;
import cn.community.system.service.IDepartmentService;
import cn.community.system.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DepartmentServiceImpl implements IDepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;

    @Autowired
    private AssociationMapper associationMapper;

    @Override
    public void delete(Long id) {
        departmentMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void save(Department department) {
        Long id = department.getAssociationId();
        String username = associationMapper.selectByPrimaryKey(id).getUsername();
        department.setAssociation(username);
        departmentMapper.insert(department);
    }

    @Override
    public Department get(Long id) {
        return departmentMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Department> listAll() {
        return departmentMapper.selectAll();
    }

    @Override
    public void update(Department department) {
        Long id = department.getAssociationId();
        String username = associationMapper.selectByPrimaryKey(id).getUsername();
        department.setAssociation(username);
        departmentMapper.updateByPrimaryKey(department);
    }

    public PageInfo query(QueryObject qo) {
        //使用分页插件,传入当前页,每页显示数量
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        //查询结果集
        List<Department> list = departmentMapper.selectForList(qo);
        return new PageInfo(list);
    }

    public PageInfo queryAsso(DepartmentQuery qo) {
        Long id = UserContext.getCurrentAssociation().getId();
        qo.setAssoId(id);
        //使用分页插件,传入当前页,每页显示数量
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        //查询结果集
        List<Department> list = departmentMapper.selectForAssoList(qo);
        return new PageInfo(list);
    }

    @Override
    public Long findIdByName(String applicant) {
        return departmentMapper.findIdByDepartment(applicant);
    }

    @Override
    public Department getByAssoId(Long id) {
        return departmentMapper.getByAssoId(id);
    }

    @Override
    public Department listDeptByAssoId(Long id) {
        return departmentMapper.listDeptByAssoId(id);
    }

    @Override
    public void saveAsso(Department department) {
        Association association = UserContext.getCurrentAssociation();
        department.setAssociationId(association.getId());
        department.setAssociation(association.getUsername());
        departmentMapper.insert(department);
    }

    @Override
    public void updateAsso(Department department) {
        Association association = UserContext.getCurrentAssociation();
        department.setAssociationId(association.getId());
        department.setAssociation(association.getUsername());
        departmentMapper.updateByPrimaryKey(department);
    }

    @Override
    public Department getByAssoIdAndDeptname(Long assoId, String deptname) {
        return departmentMapper.getByAssoIdAndDeptname(assoId,deptname);
    }

}
